
using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;

namespace ProcessesSMS
{
	public enum SMS_MsgType
	{
		SMS_TINHTIEN=1,
		SMS_KHONGTINHTIEN=0
	}
	public enum SMS_ContentType
	{
		SMS_TEXT=0,
		SMS_RINGTONE=1,
		SMS_LOGO=2,
		SMS_PICMSG=4,
		SMS_WAPPUSH=8
	}
	public class SMS_Message
	{
		private System.String _Message;
		public System.String Message
		{ 
			get 
			{ 
				return _Message;
			}
			set 
			{ 
				_Message = value;
			}
		}
		private SMS_ContentType _ContentType;
		public SMS_ContentType ContentType
		{ 
			get 
			{ 
				return _ContentType;
			}
			set 
			{ 
				_ContentType = value;
			}
		}
		private System.String _UserID;
		public System.String UserID
		{ 
			get 
			{ 
				return _UserID;
			}
			set 
			{ 
				_UserID = value;
			}
		}
		public SMS_Message()
		{}
		public SMS_Message(string UserID,string Message,SMS_ContentType ContentType)
		{
			_Message=Message;
			_ContentType=ContentType; 
			_UserID=UserID;
		}

	}
/// <summary>
/// SMS_MT
/// </summary>
	public class SMS_MT
	{

		#region ***** fields & properties ***** 

		private System.Int32 _MO_ID;
		public System.Int32 MO_ID
		{ 
			get 
			{ 
				return _MO_ID;
			}
			set 
			{ 
				_MO_ID = value;
			}
		}


		private System.String _ServiceID;
		public System.String ServiceID
		{ 
			get 
			{ 
				return _ServiceID;
			}
			set 
			{ 
				_ServiceID = value;
			}
		}

		private System.String _CommandCode;
		public System.String CommandCode
		{ 
			get 
			{ 
				return _CommandCode;
			}
			set 
			{ 
				_CommandCode = value;
			}
		}
		
		
		private System.Int32 _RequestID;
		public System.Int32 RequestID
		{ 
			get 
			{ 
				return _RequestID;
			}
			set 
			{ 
				_RequestID = value;
			}
		}
		private bool SetMsgType=false;
		private SMS_MsgType _MsgType;
		public SMS_MsgType MsgType
		{ 
			get 
			{ 
				return _MsgType;
				
			}
			set 
			{ 
				_MsgType = value;
				SetMsgType=true;
			}
		}

		private System.DateTime _RequestTime;
		public System.DateTime RequestTime
		{ 
			get 
			{ 
				return _RequestTime;
			}
			set 
			{ 
				_RequestTime = value;
			}
		}
		private ArrayList Messages = new ArrayList();
		#endregion

		/// <summary>
		/// SMS_MT
		/// </summary>
		public SMS_MT()
		{
		}


		/// <summary>
		/// SMS_MT
		/// </summary>
		public SMS_MT(System.Int32 MO_ID,System.String ServiceID,System.String CommandCode,System.Int32 RequestID,System.DateTime RequestTime)
		{
			this.MO_ID = MO_ID;
			this.ServiceID = ServiceID;
			this.CommandCode = CommandCode;
			this.RequestID = RequestID;
			this.RequestTime = RequestTime;
		}
		public SMS_MT(SMS_MO mo)
		{
			this.MO_ID = mo.MO_ID;
			this.ServiceID = mo.ServiceID;
			this.CommandCode = mo.CommandCode;
			this.RequestID = mo.RequestID;
			this.RequestTime = mo.RequestTime;
		}
		#region *****persistance managing methods
		public void AddMessage(SMS_Message msg)
		{
			Messages.Add(msg);
		}
		public void AddMessage(string UserID,string Message,SMS_ContentType ContentType)
		{
			AddMessage(new SMS_Message(UserID,Message,ContentType));
		}
		/// <summary>
		/// add a record
		/// </summary>
		/// <param name="myID"></param>
		public void Insert()
		{
			if (Messages.Count==0)
			{
				throw new Exception("Message can not Null");
			}
			if (!SetMsgType)
			{
				throw new Exception("MsgType can not Null");
			}
			string connstr = ConfigSettings.ReadSetting("SMSConnStr");
			string reqSQL = "INSERT INTO SMS_MT(MO_ID,UserID,ReceiverUserID,ServiceID,CommandCode,Message,RequestID,MsgType,ContentType,RequestTime)";
				reqSQL+= " VALUES(@MO_ID,@UserID,@ReceiverUserID,@ServiceID,@CommandCode,@Message,@RequestID,@MsgType,@ContentType,@RequestTime)";
			try
			{
				SqlConnection conn = new SqlConnection(connstr);
				conn.Open();
				SqlCommand oCommand;
				byte tmpMsgType=0;
				for(int i=0;i<Messages.Count;i++)
				{
					if (i==0) tmpMsgType=(byte)_MsgType;
					else tmpMsgType=0;
					oCommand = new SqlCommand(reqSQL,conn);
					oCommand.Parameters.Clear();
					oCommand.Parameters.AddWithValue("@MO_ID",_MO_ID);
					oCommand.Parameters.AddWithValue("@UserID",((SMS_Message)Messages[i]).UserID);
                    oCommand.Parameters.AddWithValue("@ReceiverUserID", "");
                    oCommand.Parameters.AddWithValue("@ServiceID", _ServiceID);
                    oCommand.Parameters.AddWithValue("@CommandCode", _CommandCode);
                    oCommand.Parameters.AddWithValue("@Message", ((SMS_Message)Messages[i]).Message);
                    oCommand.Parameters.AddWithValue("@RequestID", _RequestID);
                    oCommand.Parameters.AddWithValue("@MsgType", tmpMsgType);
                    oCommand.Parameters.AddWithValue("@ContentType", ((SMS_Message)Messages[i]).ContentType);
                    oCommand.Parameters.AddWithValue("@RequestTime", _RequestTime);
					oCommand.ExecuteNonQuery();
				}
				conn.Close();	
			}
			catch(Exception myErr)
			{
				throw(new Exception(myErr.ToString() + reqSQL));
			}
		}
		#endregion
	}
 }
